package mutilthread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025—03—26
 * Time: 19:50
 */
public class Practice2 {
    private static Object Locker1 = new Object();
    private static Object Locker2 = new Object();
    private static Object Locker3 = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (Locker3) {
                    try {
                        Locker3.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print("A");
                synchronized (Locker1) {
                    Locker1.notify();
                }
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (Locker1) {
                    try {
                        Locker1.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print("B");
                synchronized (Locker2){
                    Locker2.notify();
                }
            }
        });

        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (Locker2) {
                    try {
                        Locker2.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("C");
                synchronized (Locker3) {
                    Locker3.notify();
                }
            }
        });

        t1.start();
        t2.start();
        t3.start();
        //主线程加个 sleep， 确保所有线程都已经执行到wait了
        Thread.sleep(100);

        //主线程去唤醒 t1
        synchronized (Locker3) {
            Locker3.notify();
        }
    }
}
